﻿@page "/datagrid-virtualization-loaddata"
@using System.Linq.Dynamic.Core
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext

<h1>DataGrid <strong>Custom Virtualization</strong></h1>

<p>
    RadzenDataGrid supports virtualization with custom data-binding scenarios. Handle the <code>LoadData</code> event as usual.
</p>
<RadzenExample Name="DataGridVirtualizationLoadData" Documentation="false" Heading="false">
    <RadzenDataGrid Data="@orderDetails" Count="@count" LoadData="@LoadData" TItem="OrderDetail" AllowVirtualization="true" Style="height:400px"
                    AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" LogicalFilterOperator="LogicalFilterOperator.Or"
                    AllowSorting="true">
        <Columns>
            <RadzenDataGridColumn TItem="OrderDetail" Property="OrderID" Title="OrderID" />
            <RadzenDataGridColumn TItem="OrderDetail" Property="ProductID" Title="ProductID" />
            <RadzenDataGridColumn TItem="OrderDetail" Property="UnitPrice" Title="Unit Price">
                <Template Context="detail">
                    @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", detail.UnitPrice)
                </Template>
            </RadzenDataGridColumn>
            <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" />
            <RadzenDataGridColumn TItem="OrderDetail" Property="Discount" Title="Discount">
                <Template Context="detail">
                    @String.Format("{0}%", detail.Discount * 100)
                </Template>
            </RadzenDataGridColumn>
        </Columns>
    </RadzenDataGrid>
</RadzenExample>
@code {
    int count;
    IEnumerable<OrderDetail> orderDetails;

    void LoadData(LoadDataArgs args)
    {
        var query = dbContext.OrderDetails.AsQueryable();

        if (!string.IsNullOrEmpty(args.Filter))
        {
            query = query.Where(args.Filter);
        }

        if (!string.IsNullOrEmpty(args.OrderBy))
        {
            query = query.OrderBy(args.OrderBy);
        }

        orderDetails = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();

        count = dbContext.OrderDetails.Count();
    }
}